/*
 *Validatefw is a library to validation HTML forms
 *Copyright (C) 2012  Blas Soto Muñoz
 *
 *This library is free software; you can redistribute it and/or
 *modify it under the terms of the GNU Library General Public
 *License as published by the Free Software Foundation; either
 *version 2 of the License, or (at your option) any later version.

 *This library is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *Library General Public License for more details.

 *You should have received a copy of the GNU Library General Public
 *License along with this library; if not, write to the
 *Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *Boston, MA  02110-1301, USA.
 *
 **/

function Validatefw(msg_box_id_) {
    this.msg_box_id = msg_box_id_;
    this.class_error = "error";
    this.lenght_max = 255;
    /*  
     *  Valida un item input text en especial
     *  this.msg_box_id: Objeto jQuery del cuadro de mensage, un div.
     *  lenght_max: define el tamaño maximo de la variable.
     *  required: define si el campo es requerido o puede ser en blanco.
     *  
     **/

    this.setClassNameError = function(class_name) {
        this.class_error = class_name;
    }
    
    this.setLenghtMax = function(lenght_max_) {
        this.lenght_max = lenght_max_;
    }

    this.validatefw = function(obj_var, required) {
    	
        this.msg_box_id.hide();
        
        this.msg_box_id.html("");
        obj_var.attr("style","");
        
        var var_value = obj_var.val();
        
        if(var_value == null) {
            return false;
        }
        
        if(var_value.length > this.lenght_max) {
            obj_var.attr("style","border-color: red");
            this.errorMessagefw("Error, se a superado el mataño máximo de caracteres");
            return false;
        }
        if(required != undefined) {
            if(required) {
                if(var_value == "") {
                    obj_var.attr("style","border-color: red");
                    this.errorMessagefw("Error, el campo no a sido definido");
                    return false;
                }
            }
        }
        
        return true;
    }

    /*  
     *  Cuadro de mensajes de error en la clase msg_validatefw
     *  Identificar los item a validar con la clase item_validatefw     *  
     *  Retorna true si todos los items fueron validados correctamente de lo contrario retorna false
     **/
    
    this.validatefwAll = function(item_validate) {
    	var l = item_validate.length;
    	
    	for(i =0; i < l; i++){
    		if(!this.validatefw($(item_validate[i]),true))
    			return false;
    	}
    	
        return true;
    }
    
    /*
     * Mensajes de error sobre un div
     * this.msg_box_id: JQuery Object de un div donde mostrar los errores
     * message: mensaje de error
     **/
    
    this.errorMessagefw = function(message) {
        this.msg_box_id.attr("class",this.class_error);
        this.msg_box_id.html(message);
        this.msg_box_id.slideDown().delay(4000).slideUp();
    }
    
    this.validatefwMail = function(obj_var) {
        var regex_mail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if(!regex_mail.test(obj_var.val())) {
            obj_var.attr("style","border-color: red");
            this.errorMessagefw("Email invalido");
            return false;
        }
        return true;
    }
    
    this.validatefwPassword = function(passwd1,passwd2,min_length,max_length) {
        if(max_length != undefined) {
            if(passwd1.val().length > max_length) {
                passwd1.attr("style","border-color: red");
                this.errorMessagefw("La cantidad maxima de caracteres para una contraseña son "+max_length);
                return false;
            }

            if(passwd2.val().length > max_length) {
                passwd2.attr("style","border-color: red");
                this.errorMessagefw("La cantidad maxima de caracteres para una contraseña son "+max_length);
                return false;
            }
        }
        
        if(min_length != undefined) {
            if(passwd1.val().length < min_length) {
                passwd1.attr("style","border-color: red");
                this.errorMessagefw("La cantidad minima de caracteres para una contraseña son "+min_length);
                return false;
            }

            if(passwd2.val().length < min_length) {
                passwd2.attr("style","border-color: red");
                this.errorMessagefw("La cantidad minima de caracteres para una contraseña son "+min_length);
                return false;
            }
        }
        
        if(passwd1.val() != passwd2.val()) {
            passwd1.attr("style","border-color: red");
            passwd2.attr("style","border-color: red");
            this.errorMessagefw("Contraseñas no coinciden");
            return false;
        }
        return true;
    }
    
    this.validateUrl = function(obj_var) {
        var regex_mail = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
        if(!regex_mail.test(obj_var.val())) {
            obj_var.attr("style","border-color: red");
            this.errorMessagefw("URL Invalido");
            return false;
        }
        return true;
    }
    
    this.validaLengthEspecifico = function (obj_var,length_especifico) {
        this.msg_box_id.hide();
        this.msg_box_id.html("");
        $("input").attr("style","");
        
        var var_value = obj_var.val();
        
        if(var_value == null) {
            return false;
        }
        
        if(var_value.length > length_especifico) {
            obj_var.attr("style","border-color: red");
            this.errorMessagefw("Error, el tamaño maximo es de "+length_especifico+" caracteres");
            return false;
        }
        return true;
    }
}